<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class RememberToken extends Model
{
    protected $fillable = [
        'user_id', 'token', 'login_time', 'login_ip'
    ];

    public $timestamps = false;

    public static function token($token, $user_id, $login_ip)
    {
        $userToken = static::query()->firstOrNew([
            "user_id" => $user_id
        ]);
        $userToken->token = $token;
        $userToken->login_ip = $login_ip;
        $userToken->login_time = (string)now();
        $userToken->save();
    }

    public static function isLogin()
    {
        $token = request()->header("token");

        $exists = static::query()
            ->where("token", $token)
            ->first();

        if ($exists) {
            return true;
        }

        return false;
    }
}


